package com.pro.service.imple;

import com.pro.common.GuavaCommon;
import com.pro.mapper.JsCourseCategroyMapper;
import com.pro.mapper.JsProductCategroyMapper;
import com.pro.pojo.JsGoodsCount;
import com.pro.pojo.JsProductCategroy;
import com.pro.service.JsCourseCategroyService;
import com.pro.service.JsProductCategroyService;
import com.pro.utils.GuavaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Service
public class JsCourseCategroyServiceImple implements JsCourseCategroyService {
    @Autowired(required = false)
    private JsCourseCategroyMapper mapper;
    //获取数据
    @Override
    public List<JsProductCategroy> getTree() {
        try{
            Object obj = GuavaUtil.getTenCacheBuild().get(String.valueOf(GuavaCommon.CourseCategory), () -> {
                List<JsProductCategroy> list = mapper.getTree();
                JsProductCategroy jsProductCategroy = new JsProductCategroy();
                jsProductCategroy.setCategroyId(0);
                jsProductCategroy.setTitle("选择分类类型");
                jsProductCategroy.setParentId(-1);
                list.add(jsProductCategroy);
                return list;
            });
            if (obj instanceof List){
                List<JsProductCategroy> list=(List<JsProductCategroy>)obj;
                return list;
            }
        }catch (ExecutionException e){
            e.printStackTrace();
        }
        return mapper.getTree();
    }

    //添加数据节点
    @Override
    public Boolean addCourseClass(JsProductCategroy JsProductCategroy) {
        Boolean flag =mapper.addCourseClass(JsProductCategroy);
        if (flag){
            GuavaUtil.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategory));
        }
        return flag;
    }

    //删除节点
    @Override
    public Boolean delCourseClass(Integer categroyId) {
        Boolean flag =mapper.delCourseClass(categroyId);
        if (flag){
            GuavaUtil.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategory));
        }
        return flag;
    }

    @Override
    public Boolean updateCourseClass(JsProductCategroy jsProductCategroy) {
        Boolean flag =mapper.updateCourseClass(jsProductCategroy);
        if (flag){
            GuavaUtil.getTenCacheBuild().invalidate(String.valueOf(GuavaCommon.ProductCategory));
        }
        return flag;
    }

    @Override
    public List<JsGoodsCount> likeSum() {
        return mapper.likeSum();
    }

    @Override
    public List<JsGoodsCount> collectionCount() {
        return mapper.collectionCount();
    }

    @Override
    public List<JsGoodsCount> shareSum() {
        return mapper.shareSum();
    }

    @Override
    public List<JsGoodsCount> lookSum() {
        return mapper.lookSum();
    }

    @Override
    public List<JsGoodsCount> commentSum() {
        return mapper.commentSum();
    }
}
